package com.service.manage.imp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mapper.manage.RoleDao;
import com.mapper.manage.UserDao;
import com.mapper.manage.UserRegisterDao;
import com.mapper.manage.UserRoleDao;
import com.mapper.manage.UserZJDao;
import com.po.UserRegister;
import com.po.UserRole;
import com.po.UserZJ;
import com.service.manage.UserRegisterService;
import com.service.manage.UserZJService;
@Service
public class UserZJServiceImp implements UserZJService{
    @Resource(name="userZJDao")
    private UserZJDao userZJDao;
    
	@Resource(name = "userRegisterServiceImp")
	private UserRegisterService userRegisterService;
	@Resource(name = "userRoleDao")
	private UserRoleDao userRoleDao;
	@Resource(name="roleDao")
	private RoleDao roleDao;
	@Resource(name = "userDao")
	private UserDao userDao;
	@Resource(name = "userRegisterDao")
	private UserRegisterDao userRegisterDao;
    
    
	@Override
	public List<UserZJ> getUserZJList(UserZJ userzj) {
		List<UserZJ> list=userZJDao.getUserZJList(userzj);
		return list;
	}

	@Override
	public Integer getUserZJCount(UserZJ userzj) {
		int i=userZJDao.getUserZJCount(userzj);
		return i;
	}

	@Override
	public int deleteUserZJ(String userzjid) {
		int i=userZJDao.deleteUserZJ(userzjid);
		return i;
	}

	@Override
	public int deleteUserZJList(String[] userzjid) {
		int i=userZJDao.deleteUserZJList(userzjid);
		return i;
	}

	@Override
	public int addUserZJ(UserZJ userzj) {
		UserRegister userRegister1=new UserRegister();
		userRegister1.setUser_id(userzj.getUserzj_id());
		UserRegister userRegister2=userRegisterService.addUserRegisterIsOk(userRegister1);		
		if(userRegister2==null){
			//正常插入一条数据
			UserRegister userRegister=new UserRegister();
			userRegister.setUser_id(userzj.getUserzj_id());
			userRegister.setUser_name(userzj.getUserzj_name());
			userRegister.setUser_type(roleDao.getRoleID("专家"));
			userRegister.setUser_validate(userzj.getUserzj_validate());
			
			UserRole userRole=new UserRole(); 
			userRole.setUser_id(userzj.getUserzj_id());
			userRole.setRole_id(roleDao.getRoleID("专家"));
			int result1 = userRegisterService.addUserRegister(userRegister);
			int result2 = userZJDao.addUserZJ(userzj);	
			int result3 = userRoleDao.addUserRole(userRole);
			/*int resultb=0;
			//需要重新更新生日
			if(userzj.getUserzj_birth()!=null&&userzj.getUserzj_birth()!="")
				resultb=userZJDao.updateUserZJBirth(userzj);
			else {
				resultb=1;
			}*/	
			if(result1==1&&result2==1&&result3==1)
				return 1;
			else 
				return 0;
		}
		//表中已经存在一条该ID的用户，但是是无效用户
		else{
			//如果这条无效数据类型为专家
			if(!userRegister2.getUser_type().equals(roleDao.getRoleID("专家"))){
				int result4=userDao.deleteUserT(userzj.getUserzj_id());
				
				UserRegister userRegister=new UserRegister();
				userRegister.setUser_id(userzj.getUserzj_id());
				userRegister.setUser_name(userzj.getUserzj_name());
				userRegister.setUser_type(roleDao.getRoleID("专家"));
				userRegister.setUser_validate(userzj.getUserzj_validate());
				
				UserRole userRole=new UserRole(); 
				userRole.setUser_id(userzj.getUserzj_id());
				userRole.setRole_id(roleDao.getRoleID("专家"));
				int result1 = userRegisterService.updateUserRegister(userRegister);
				int result2 = userZJDao.addUserZJ(userzj);	
				int result3 = userRoleDao.updateUserRole(userRole);
				/*int resultb=0;
				if(userzj.getUserzj_birth()!=null&&userzj.getUserzj_birth()!="")
					resultb=userZJDao.updateUserZJBirth(userzj);
				else {
					resultb=1;
				}*/
				if(result1==1&&result2==1&&result3==1&&result4==1)
					return 1;
				else 
					return 0;	
			}
			else{				
				UserRegister userRegister=new UserRegister();
				userRegister.setUser_id(userzj.getUserzj_id());
				userRegister.setUser_name(userzj.getUserzj_name());
				userRegister.setUser_type(roleDao.getRoleID("专家"));
				userRegister.setUser_validate(userzj.getUserzj_validate());
				
				UserRole userRole=new UserRole(); 
				userRole.setUser_id(userzj.getUserzj_id());
				userRole.setRole_id(roleDao.getRoleID("专家"));
				int result1 = userRegisterService.updateUserRegister(userRegister);
				int result2 = userZJDao.updateUserZJ(userzj);	
				int result3 = userRoleDao.updateUserRole(userRole);
				/*int resultb=0;
				if(userzj.getUserzj_birth()!=null&&userzj.getUserzj_birth()!="")
					resultb=userZJDao.updateUserZJBirth(userzj);
				else {
					resultb=1;
				}*/
				if(result1==1&&result2==1&&result3==1)
					return 1;
				else 
					return 0;
			}
		}				
	}

	@Override
	public int updateUserZJ(UserZJ userzj) {
		UserRegister userRegister=new UserRegister();
		userRegister.setUser_id(userzj.getUserzj_id());
		userRegister.setUser_name(userzj.getUserzj_name());
		userRegister.setUser_type(roleDao.getRoleID("专家"));
		userRegister.setUser_validate(userzj.getUserzj_validate());
		
		int result1 = userRegisterDao.updateUserRegister(userRegister);
		int result2 = userZJDao.updateUserZJ(userzj);
		int resultb=0;
		if(userzj.getUserzj_birth()!=null)
			resultb=userZJDao.updateUserZJBirth(userzj);
		else {
			resultb=1;
		}
		if(result1==1&&result2==1&&resultb==1)
			return 1;
		else 
			return 0;
	}

	@Override
	public int deleteUserZJT(String userzjid) {
		int i=userZJDao.deleteUserZJT(userzjid);
		return i;
	}

}
